package resources

import (
	"log"
	"net/http"

	"github.com/xi3/qshop/Godeps/_workspace/src/github.com/qor/qor"
	"github.com/xi3/qshop/Godeps/_workspace/src/github.com/qor/qor/roles"

	"github.com/xi3/qshop/models"
)

func roleRegisterFunc(role string) func(req *http.Request, currentUser qor.CurrentUser) bool {
	log.Println("role: " + role + "registered")
	return func(req *http.Request, currentUser qor.CurrentUser) bool {
		if currentUser == nil {
			return false
		}

		if currentUser.(*models.User).Role == role {
			return true
		}

		return false
	}
}

func init() {
	roles.Register("owner", roleRegisterFunc("owner"))
	roles.Register("administrator", roleRegisterFunc("administrator"))
	roles.Register("customer", roleRegisterFunc("customer"))
}
